package com.greenantmall.activity;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVMobilePhoneVerifyCallback;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.SignUpCallback;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.greenantmall.R;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.NotificationUtil;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class LoginActivity extends BaseActivity {
	private View mBackView;
	private TextView mTvTopDes;

	private LogInManage mLogInManage;
	private RegisterManage mRegisterManage;
	private ForgotPasswordManage mForgotPasswordManage;

	private final String LIMIT_REGULAR_EXPRESSION_PHONE = "^1[3-8]+\\d{9}$";
	
	private Handler mHandler = new Handler(Looper.getMainLooper());

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		 SystemBarTintManager.setContentView(this, R.layout.activity_login, getResources().getColor(R.color.green));
		mBackView = findViewById(R.id.activity_login_top_arrow_left);
		mTvTopDes = (TextView)findViewById(R.id.activity_login_top_des);
  
		mLogInManage = new LogInManage();
		mRegisterManage = new RegisterManage();
		mForgotPasswordManage = new ForgotPasswordManage();

		this.displayLayoutLogIn();
	}

	private void displayLayoutLogIn() {
		mLogInManage.display();
		mRegisterManage.hide();
		mForgotPasswordManage.hide();

		mBackView.setOnClickListener(mLogInManage);
		mTvTopDes.setText(R.string.activity_login_login);
	}

	private void displayLayoutRegister() {
		mLogInManage.hide();
		mRegisterManage.display();
		mForgotPasswordManage.hide();

		mBackView.setOnClickListener(mRegisterManage);
		mTvTopDes.setText(R.string.activity_login_new_user_register);
	}

	private void displayLayoutForgotPassword() {
		mLogInManage.hide();
		mRegisterManage.hide();
		mForgotPasswordManage.display();

		mBackView.setOnClickListener(mForgotPasswordManage);
		mTvTopDes.setText(R.string.activity_login_find_password);
	}

	private class LogInManage implements OnClickListener {
		private View mLayoutLogIn;
		private EditText mEtPhone;
		private EditText mEtPassword;
		private Button mBtnLogin;
		private Button mBtnNewUserRegister;
		private Button mBtnForgotPassword;

		public LogInManage() {
			mLayoutLogIn = findViewById(R.id.activity_login_layout_login);
			mEtPhone = (EditText) findViewById(R.id.activity_login_layout_login_et_userName);
			mEtPassword = (EditText) findViewById(R.id.activity_login_layout_login_et_password);
			mBtnLogin = (Button) findViewById(R.id.activity_login_layout_login_btn_login);
			mBtnNewUserRegister = (Button) findViewById(R.id.activity_login_layout_login_btn_new_user_register);
			mBtnForgotPassword = (Button) findViewById(R.id.activity_login_layout_login_btn_forget_password);

			mBtnLogin.setOnClickListener(this);
			mBtnNewUserRegister.setOnClickListener(this);
			mBtnForgotPassword.setOnClickListener(this);
		}

		public void display() {
			mLayoutLogIn.setVisibility(View.VISIBLE);
		}

		public void hide() {
			mLayoutLogIn.setVisibility(View.GONE);
		}

		@Override
		public void onClick(View v) {
			if (v.equals(mBackView)) {
				LoginActivity.this.finish();
			} else if (v.equals(mBtnLogin)) {
				String phone = mEtPhone.getText().toString();
				if (TextUtils.isEmpty(phone)
						|| !phone.matches(LIMIT_REGULAR_EXPRESSION_PHONE)) {
					UtilTools
							.showMyToast(
									LoginActivity.this,
									getResources()
											.getString(
													R.string.activity_login_retrieve_error_phone));
					return;
				}

				String password = mEtPassword.getText().toString();
				if (TextUtils.isEmpty(password) || password.length() < 6) {
					UtilTools
							.showMyToast(
									LoginActivity.this,
									getResources()
											.getString(
													R.string.activity_login_retrieve_error_password));
					return;
				}
				NotificationUtil.unRegisterNotificationWhenUserChange(LoginActivity.this);
				AVUser.loginByMobilePhoneNumberInBackground(phone, password,
						new LogInCallback<AVUser>() {
							@Override
							public void done(AVUser user, AVException e) {
								if (e == null && user != null) {
									Log.d("CEXX", "CEXX ---> loginByMobilePhoneNumberInBackground ---> loginSuccessful");
									UtilTools.showMyToast(LoginActivity.this, 
											getResources().getString(R.string.activity_login_login_successful));
									//登陆成功之后，用户和设备关联
									if(!TextUtils.isEmpty(AntHomeApplication.installationId))
									{
									    user.put(Const.AVUSER_KEY_INSTALLID, AntHomeApplication.installationId);
									    user.saveInBackground();
									    Log.d("CEXX", "CEXX ---> loginByMobilePhoneNumberInBackground ---> installationId put successful");
									}
									NotificationUtil.registerNotificationReceiver(LoginActivity.this);
									LoginActivity.this.finish();
								} else if (user == null) {
									Log.d("CEXX", "CEXX ---> loginByMobilePhoneNumberInBackground ---> usernameOrPasswordIsInvalid" + e.toString());
									UtilTools.showMyToast(LoginActivity.this, 
											getResources().getString(R.string.activity_login_login_account_error));
								} else {
									Log.d("CEXX", "CEXX ---> loginByMobilePhoneNumberInBackground ---> somethingWentWrong : " + e.toString());
									UtilTools.showMyToast(LoginActivity.this, 
											getResources().getString(R.string.activity_login_login_failed));
								}
							}
						});
			} else if (v.equals(mBtnNewUserRegister)) {
				displayLayoutRegister();
			} else if (v.equals(mBtnForgotPassword)) {
				displayLayoutForgotPassword();
			}
		}
	}

	private class RegisterManage implements OnClickListener {
		private View mStep0_LayoutRegister;
		private EditText mStep0_EtPhone;
		private EditText mStep0_EtRecommend;
		private Button mStep0_BtnNextStep;

		private View mStep1_LayoutRegister;
		private EditText mStep1_EtVerificationCode;
		private Button mStep1_BtnRetrieveVerificationCode;
		private Button mStep1_BtnNextStep;

		private View mStep2_LayoutRegister;
		private EditText mStep2_EtPassword;
		private EditText mStep2_EtPasswordEnsure;
		private Button mStep2_BtnSure;
		
		private String mPhoneRecommend;
		private String mPhoneNumber;
		private Timer mTimer;
		
		private OnClickListener mClickListenerStep0 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep0_BtnNextStep)) {
					// check phone
					final String phone = mStep0_EtPhone.getText().toString();
	                if(TextUtils.isEmpty(phone) || !phone.matches(LIMIT_REGULAR_EXPRESSION_PHONE))
	                {
	                    UtilTools.showMyToast(LoginActivity.this, 
	                    		getResources().getString(R.string.activity_login_retrieve_error_phone));
	                    return;
	                }
	                // check recommend
	                mPhoneRecommend = mStep0_EtRecommend.getText().toString();
	                if(!TextUtils.isEmpty(mPhoneRecommend) && !mPhoneRecommend.matches(LIMIT_REGULAR_EXPRESSION_PHONE))
	                {
	                    UtilTools.showMyToast(LoginActivity.this, 
	                    		getResources().getString(R.string.activity_login_retrieve_error_phone_recommend));
	                    return;
	                }
	                // check phone exist or not
	                AVQuery<AVUser> query = AVUser.getQuery();
	                query.whereEqualTo("username", phone);
	                query.whereEqualTo("mobilePhoneNumber", phone);
	                query.findInBackground(new FindCallback<AVUser>() {
						@Override
						public void done(List<AVUser> list, AVException e) {
							Log.d("CEXX", "CEXX ---> query successful --->  list : " + (list==null ? list : list.size()));
							Log.d("CEXX", "CEXX ---> query successful --->  e : " + e);
							
							if (e == null && list != null && list.size() > 0) {
	                            // query successful
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_register_phone_registered));
	                        } else {
	                            // query failed
	                        	if(e != null) {
	                        		UtilTools.showMyToast(LoginActivity.this, 
	                        				getResources().getString(R.string.activity_login_register_check_phone_error)
	                        				+ "(" + e.getCode() + ")");
	                        	}
	                        	else {
	                        		mStep0_LayoutRegister.setVisibility(View.GONE);
	                			    mStep1_LayoutRegister.setVisibility(View.VISIBLE);
	                			    mStep2_LayoutRegister.setVisibility(View.GONE);
	                			    
	                			    mPhoneNumber = phone;
	                			    retrieveVerificationCodeCountDown();
	                        	}
	                        }
						}
					});
				}
			}
		};
		
		private OnClickListener mClickListenerStep1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep1_BtnRetrieveVerificationCode)) {
					retrieveVerificationCodeCountDown();
				}
				else if(v.equals(mStep1_BtnNextStep)) {
					String code = mStep1_EtVerificationCode.getText().toString();
					if(TextUtils.isEmpty(code)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_register_input_verification_code));
						return;
					}
					
					AVOSCloud.verifySMSCodeInBackground(code, mPhoneNumber, new AVMobilePhoneVerifyCallback() {
						@Override
						public void done(AVException e) {
							if(e == null) {
								mStep0_LayoutRegister.setVisibility(View.GONE);
				                mStep1_LayoutRegister.setVisibility(View.GONE);
				                mStep2_LayoutRegister.setVisibility(View.VISIBLE);
							}
							else {
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_register_verification_code_error));
							}
						}
					});
				}
			}
		};
		
		private OnClickListener mClickListenerStep2 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep2_BtnSure)) {
					String passwordStr = mStep2_EtPassword.getText().toString();
					if(TextUtils.isEmpty(passwordStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_input_user_password));
						return;
					}
					
					String passwordEnsureStr = mStep2_EtPasswordEnsure.getText().toString();
					if(TextUtils.isEmpty(passwordEnsureStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_input_user_password_ensure));
						return;
					}
					
					if(!passwordEnsureStr.equals(passwordStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_register_password_not_equal));
						return;
					}
					
					AVUser avUser = new AVUser();
					avUser.setUsername(mPhoneNumber);
					avUser.setMobilePhoneNumber(mPhoneNumber);
					avUser.setPassword(passwordStr);
					avUser.signUpInBackground(new SignUpCallback() {
						@Override
						public void done(AVException e) {
							if(e == null) {
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_register_register_successful));
								
								if(!TextUtils.isEmpty(mPhoneRecommend)) {
									AVUser avUser = AVUser.getCurrentUser();
									if(avUser != null) {
										avUser.put("recommend", mPhoneRecommend);
										avUser.saveInBackground();
									}
								}
								
//								// 转到登录界面
//								displayLayoutLogIn();
								LoginActivity.this.finish();
							}
							else {
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_register_register_failed));
								Log.d("CEXX", "CEXX ---> signUpInBackground ---> e : " + e.toString());
							}
						}
					});
				}
			}
		};
		
		private void retrieveVerificationCodeCountDown() {
			if(mTimer != null) {
				mTimer.cancel();
			}
			
			mStep1_BtnRetrieveVerificationCode.setEnabled(false);
    		final String formatStr = getResources().getString(
    				R.string.activity_login_retrieve_verification_code_count_down);
    		mStep1_BtnRetrieveVerificationCode.setText(String.format(formatStr, 60));
    		mTimer = new Timer();
   		    mTimer.schedule(new TimerTask() {
   		    	int totalCount = 60;
   				@Override
   				public void run() {
   					mHandler.post(new Runnable() {
   						@Override
   						public void run() {
   							mStep1_BtnRetrieveVerificationCode.setText(String.format(formatStr, --totalCount));
   							if(totalCount == 0) {
   								mTimer.cancel();
   								mStep1_BtnRetrieveVerificationCode.setEnabled(true);
   								mStep1_BtnRetrieveVerificationCode.setText(
   										getResources().getString(R.string.activity_login_retrieve_verification_code));
   							}
   						}
   					});
   				}
   		    }, 1000, 1000);
			
			AVOSCloud.requestSMSCodeInBackground(mPhoneNumber, null, null, 1, new RequestMobileCodeCallback(){
		        public void done(AVException e){
		        	if(e == null){
		        		// 发送验证码成功
		        	}
		        	else {
		        		// 发送验证码失败
		        		mTimer.cancel();
		        		mStep1_BtnRetrieveVerificationCode.setEnabled(true);
		        		mStep1_BtnRetrieveVerificationCode.setText(
		        				getResources().getString(R.string.activity_login_retrieve_verification_code));
		        	}
		        }
		    });
		}

		public RegisterManage() {
			mStep0_LayoutRegister = findViewById(R.id.activity_login_layout_register_step_0);
			mStep0_EtPhone = (EditText) findViewById(R.id.activity_login_layout_register_step_0_et);
			mStep0_EtRecommend = (EditText) findViewById(R.id.activity_login_layout_register_step_0_et_recommend);
			mStep0_BtnNextStep = (Button) findViewById(R.id.activity_login_layout_register_step_0_btn);
			mStep0_BtnNextStep.setOnClickListener(mClickListenerStep0);

			mStep1_LayoutRegister = findViewById(R.id.activity_login_layout_register_step_1);
			mStep1_EtVerificationCode = (EditText) findViewById(R.id.activity_login_layout_register_step_1_et);
			mStep1_BtnRetrieveVerificationCode = (Button) findViewById(R.id.activity_login_layout_register_step_1_btn0);
			mStep1_BtnNextStep = (Button) findViewById(R.id.activity_login_layout_register_step_1_btn1);
			mStep1_BtnRetrieveVerificationCode.setOnClickListener(mClickListenerStep1);
			mStep1_BtnNextStep.setOnClickListener(mClickListenerStep1);

			mStep2_LayoutRegister = findViewById(R.id.activity_login_layout_register_step_2);
			mStep2_EtPassword = (EditText) findViewById(R.id.activity_login_layout_register_step_2_et0);
			mStep2_EtPasswordEnsure = (EditText) findViewById(R.id.activity_login_layout_register_step_2_et1);
			mStep2_BtnSure = (Button) findViewById(R.id.activity_login_layout_register_step_2_btn);
			mStep2_BtnSure.setOnClickListener(mClickListenerStep2);
		}

		public void display() {
			mStep0_LayoutRegister.setVisibility(View.VISIBLE);
			mStep1_LayoutRegister.setVisibility(View.GONE);
			mStep2_LayoutRegister.setVisibility(View.GONE);
		}

		public void hide() {
			mStep0_LayoutRegister.setVisibility(View.GONE);
			mStep1_LayoutRegister.setVisibility(View.GONE);
			mStep2_LayoutRegister.setVisibility(View.GONE);
		}

		@Override
		public void onClick(View v) {
			if(v.equals(mBackView))
        	{
        		if(mStep2_LayoutRegister.getVisibility() == View.VISIBLE)
        		{
        			mStep0_LayoutRegister.setVisibility(View.GONE);
    			    mStep1_LayoutRegister.setVisibility(View.VISIBLE);
    			    mStep2_LayoutRegister.setVisibility(View.GONE);
        		}
        		else if(mStep1_LayoutRegister.getVisibility() == View.VISIBLE)
        		{
        			mStep0_LayoutRegister.setVisibility(View.VISIBLE);
    			    mStep1_LayoutRegister.setVisibility(View.GONE);
    			    mStep2_LayoutRegister.setVisibility(View.GONE);
        		}
        		else if(mStep0_LayoutRegister.getVisibility() == View.VISIBLE)
        		{
        			displayLayoutLogIn();
        		}
        	}
		}
	}

	private class ForgotPasswordManage implements OnClickListener {
		private View mStep0_ForgotPasswordManage;
		private EditText mStep0_EtPhone;
		private Button mStep0_BtnNextStep;

		private View mStep1_ForgotPasswordManage;
		private EditText mStep1_EtPassword;
		private EditText mStep1_EtPasswordEnsure;
		private Button mStep1_BtnNextStep;
		
		private View mStep2_ForgotPasswordManage;
		private EditText mStep2_EtVerificationCode;
		private Button mStep2_BtnRetrieveVerificationCode;
		private Button mStep2_BtnEnsure;
		
		private String mPhoneNumber;
		private String mNewPassword;
		private Timer mTimer;
		
		private OnClickListener mClickListenerStep0 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep0_BtnNextStep)) {
					// check phone
					final String phone = mStep0_EtPhone.getText().toString();
	                if(TextUtils.isEmpty(phone) || !phone.matches(LIMIT_REGULAR_EXPRESSION_PHONE))
	                {
	                    UtilTools.showMyToast(LoginActivity.this, 
	                    		getResources().getString(R.string.activity_login_retrieve_error_phone));
	                    return;
	                }
	                // check phone exist or not
	                AVQuery<AVUser> query = AVUser.getQuery();
	                query.whereEqualTo("username", phone);
	                query.whereEqualTo("mobilePhoneNumber", phone);
	                query.findInBackground(new FindCallback<AVUser>() {
						@Override
						public void done(List<AVUser> list, AVException e) {
							Log.d("CEXX", "CEXX ---> query successful --->  list : " + (list==null ? list : list.size()));
							Log.d("CEXX", "CEXX ---> query successful --->  e : " + e);
							
							if (e == null && list != null && list.size() == 0) {
	                            // query successful
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_register_phone_not_registered));
								// 转到登录界面
								displayLayoutLogIn();
	                        } else {
	                            // query failed
	                        	mStep0_ForgotPasswordManage.setVisibility(View.GONE);
                			    mStep1_ForgotPasswordManage.setVisibility(View.VISIBLE);
                			    mStep2_ForgotPasswordManage.setVisibility(View.GONE);
                			    
                			    mPhoneNumber = phone;
	                        }
						}
					});
				}
			}
		};
		
		private OnClickListener mClickListenerStep1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep1_BtnNextStep)) {
					String passwordStr = mStep1_EtPassword.getText().toString();
					if(TextUtils.isEmpty(passwordStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_input_user_password_new));
						return;
					}
					
					String passwordEnsureStr = mStep1_EtPasswordEnsure.getText().toString();
					if(TextUtils.isEmpty(passwordEnsureStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_input_user_password_ensure_new));
						return;
					}
					
					if(!passwordEnsureStr.equals(passwordStr)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_register_password_not_equal));
						return;
					}
					
					mNewPassword = passwordStr;
					mStep0_ForgotPasswordManage.setVisibility(View.GONE);
	                mStep1_ForgotPasswordManage.setVisibility(View.GONE);
	                mStep2_ForgotPasswordManage.setVisibility(View.VISIBLE);
	                
	                retrieveVerificationCodeCountDown();
				}
			}
		};
		
		private OnClickListener mClickListenerStep2 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.equals(mStep2_BtnRetrieveVerificationCode)) {
					retrieveVerificationCodeCountDown();
				}
				else if(v.equals(mStep2_BtnEnsure)) {
					String code = mStep2_EtVerificationCode.getText().toString();
					if(TextUtils.isEmpty(code)) {
						UtilTools.showMyToast(LoginActivity.this, 
								getResources().getString(R.string.activity_login_register_input_verification_code));
						return;
					}
					
					AVUser.resetPasswordBySmsCodeInBackground(code, mNewPassword, new UpdatePasswordCallback() {
						@Override
						public void done(AVException e) {
							if(e == null) {
								// 转到登录界面
								displayLayoutLogIn();
							}
							else {
								Log.d("CEXX", "CEXX ---> resetPasswordBySmsCodeInBackground --->  e : " + e);
								Log.d("CEXX", "CEXX ---> resetPasswordBySmsCodeInBackground --->  e.getCode : " + e.getCode());
								UtilTools.showMyToast(LoginActivity.this, 
										getResources().getString(R.string.activity_login_forgot_password_modify_error));
							}
						}
					});
				}
			}
		};
		
		private void retrieveVerificationCodeCountDown() {
			if(mTimer != null) {
				mTimer.cancel();
			}
			
			mStep2_BtnRetrieveVerificationCode.setEnabled(false);
    		final String formatStr = getResources().getString(
    				R.string.activity_login_retrieve_verification_code_count_down);
    		mStep2_BtnRetrieveVerificationCode.setText(String.format(formatStr, 60));
    		mTimer = new Timer();
   		    mTimer.schedule(new TimerTask() {
   		    	int totalCount = 60;
   				@Override
   				public void run() {
   					mHandler.post(new Runnable() {
   						@Override
   						public void run() {
   							mStep2_BtnRetrieveVerificationCode.setText(String.format(formatStr, --totalCount));
   							if(totalCount == 0) {
   								mTimer.cancel();
   								mStep2_BtnRetrieveVerificationCode.setEnabled(true);
   								mStep2_BtnRetrieveVerificationCode.setText(
   										getResources().getString(R.string.activity_login_retrieve_verification_code));
   							}
   						}
   					});
   				}
   		    }, 1000, 1000);
   		    
   		    AVUser.requestPasswordResetBySmsCodeInBackground(mPhoneNumber, new RequestMobileCodeCallback() {
				@Override
				public void done(AVException e) {
					if(e == null){
		        		// 发送验证码成功
						
		        	}
		        	else {
		        		// 发送验证码失败
		        		Log.d("CEXX", "CEXX ---> requestPasswordResetBySmsCodeInBackground ---> e : " + e.toString());
		        		mTimer.cancel();
		        		mStep2_BtnRetrieveVerificationCode.setEnabled(true);
		        		mStep2_BtnRetrieveVerificationCode.setText(
		        				getResources().getString(R.string.activity_login_retrieve_verification_code));
		        	}
				}
			});
		}

		public ForgotPasswordManage() {
			mStep0_ForgotPasswordManage = findViewById(R.id.activity_login_layout_forgot_password_step_0);
			mStep0_EtPhone = (EditText) findViewById(R.id.activity_login_layout_forgot_password_step_0_et);
			mStep0_BtnNextStep = (Button) findViewById(R.id.activity_login_layout_forgot_password_step_0_btn);
			mStep0_BtnNextStep.setOnClickListener(mClickListenerStep0);
			
			mStep1_ForgotPasswordManage = findViewById(R.id.activity_login_layout_forgot_password_step_1);
			mStep1_EtPassword = (EditText) findViewById(R.id.activity_login_layout_forgot_password_step_1_et0);
			mStep1_EtPasswordEnsure = (EditText) findViewById(R.id.activity_login_layout_forgot_password_step_1_et1);
			mStep1_BtnNextStep = (Button) findViewById(R.id.activity_login_layout_forgot_password_step_1_btn);
			mStep1_BtnNextStep.setOnClickListener(mClickListenerStep1);

			mStep2_ForgotPasswordManage = findViewById(R.id.activity_login_layout_forgot_password_step_2);
			mStep2_EtVerificationCode = (EditText) findViewById(R.id.activity_login_layout_forgot_password_step_2_et);
			mStep2_BtnRetrieveVerificationCode = (Button) findViewById(R.id.activity_login_layout_forgot_password_step_2_btn0);
			mStep2_BtnEnsure = (Button) findViewById(R.id.activity_login_layout_forgot_password_step_2_btn1);
			mStep2_BtnRetrieveVerificationCode.setOnClickListener(mClickListenerStep2);
			mStep2_BtnEnsure.setOnClickListener(mClickListenerStep2);
		}

		public void display() {
			mStep0_ForgotPasswordManage.setVisibility(View.VISIBLE);
			mStep1_ForgotPasswordManage.setVisibility(View.GONE);
			mStep2_ForgotPasswordManage.setVisibility(View.GONE);
		}

		public void hide() {
			mStep0_ForgotPasswordManage.setVisibility(View.GONE);
			mStep1_ForgotPasswordManage.setVisibility(View.GONE);
			mStep2_ForgotPasswordManage.setVisibility(View.GONE);
		}

		@Override
		public void onClick(View v) {
			if(v.equals(mBackView))
        	{
        		if(mStep2_ForgotPasswordManage.getVisibility() == View.VISIBLE)
        		{
        			mStep0_ForgotPasswordManage.setVisibility(View.GONE);
    			    mStep1_ForgotPasswordManage.setVisibility(View.VISIBLE);
    			    mStep2_ForgotPasswordManage.setVisibility(View.GONE);
        		}
        		else if(mStep1_ForgotPasswordManage.getVisibility() == View.VISIBLE)
        		{
        			mStep0_ForgotPasswordManage.setVisibility(View.VISIBLE);
    			    mStep1_ForgotPasswordManage.setVisibility(View.GONE);
    			    mStep2_ForgotPasswordManage.setVisibility(View.GONE);
        		}
        		else if(mStep0_ForgotPasswordManage.getVisibility() == View.VISIBLE)
        		{
        			displayLayoutLogIn();
        		}
        	}
		}
	}
}
